const router = require("express").Router();
const jwt = require("jsonwebtoken");

const users = [
  {
    id: 1,
    userName: "admin",
    password: "admin",
  },
  {
    id: 2,
    userName: "admin011",
    password: "111111",
  },
];

router.post("/login", (req, res) => {
  const token = jwt.sign(
    {
      userName: "xiaozhi",
      id: 9,
    },
    "ff",
    {
      expiresIn: "10h", // 过期时间
    }
  );
  res.json({
    code: 1,
    token,
  });
});

router.post("/login_manager", (req, res) => {
  if (req.body.userName && req.body.password) {
    const user = users.find((u) => u.userName == req.body.userName);
    if (user) {
      if (user.password == req.body.password) {
        res.cookie("admin_token", "ffcmldjt12138"); // 写cookie
        res.json({
          code: 1,
          msg: "登录成功",
        });
      } else {
        res.json({
          code: 0,
          msg: "密码输入错误",
        });
      }
    } else {
      res.json({
        code: 0,
        msg: "用户名输入错误",
      });
    }
  } else {
    res.json({
      code: 0,
      msg: "用户登录信息不能为空",
    });
  }
});

module.exports = router;
